package com.lindroy.androidplayer.ui.find.fragment

import android.os.Bundle
import android.view.View
import com.lindroy.androidplayer.base.BaseListFragment
import com.lindroy.androidplayer.databinding.FragmentKnowledgeSystemBinding
import com.lindroy.androidplayer.logic.model.bean.TabBean
import com.lindroy.androidplayer.ui.adapter.KnowledgeSystemAdapter
import com.lindroy.androidplayer.ui.knowledge.KnowledgeDetailFragment
import com.lindroy.androidplayer.utils.addSimpleDivider
import com.lindroy.androidplayer.utils.collectListWhenResumed
import com.lindroy.androidplayer.utils.toArrayList
import com.lindroy.androidplayer.viewmodels.KnowledgeViewModel

/**
 * @author Lin
 * @date 2022/1/23
 * @function 体系
 * @Description
 */
class KnowledgeSystemFragment :
    BaseListFragment<FragmentKnowledgeSystemBinding, KnowledgeViewModel>() {

    private val adapter by lazy { KnowledgeSystemAdapter(viewLifecycleOwner) }

    companion object {
        @JvmStatic
        fun newInstance() =
            KnowledgeSystemFragment().apply {
                arguments = Bundle().apply {}
            }
    }

    override fun FragmentKnowledgeSystemBinding.initView(savedInstanceState: Bundle?) {
        vm = mViewMode
        rvKnowledgeSystem.addSimpleDivider()
        rvKnowledgeSystem.adapter = adapter
    }

    override fun FragmentKnowledgeSystemBinding.initListener() {
        mViewMode.setLayoutStateFlow(adapter.loadStateFlow)
        mViewMode.knowledgeSystemList.collectListWhenResumed(viewLifecycleOwner) { list ->
            adapter.submitData(list)
        }
        mViewMode.itemClickAction.observe(viewLifecycleOwner) {
            navigateToDetailPage(it)
        }
        mViewMode.childItemClickAction.observe(viewLifecycleOwner) {
            navigateToDetailPage(it)
        }
    }

    private fun navigateToDetailPage(list:List<TabBean>){
        KnowledgeDetailFragment.navigateTo(this@KnowledgeSystemFragment, list.toArrayList())
    }

    override fun onRefresh() {
        adapter.retry()
    }

    override fun onRetry(view: View) {
        adapter.retry()
    }

}